package com.example.alarmcontrolpanel;

import java.util.Calendar;

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CheckBox;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;
import android.widget.TimePicker;

public class AlarmCreator extends Activity {

	int[] day = new int[7];
	View layout;
	AlramRecord newAlarm;
	int repeatType;
	Calendar calCalculate;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_alarm_creator);
		
		checkRepeat(findViewById(R.layout.activity_alarm_creator));
		for(int i=0; i<7;i++)
			day[i]=0;
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_alarm_creator, menu);
		return true;
	}
	public boolean onOptionsItemSelected(MenuItem item)
	{
	    // Handle item selection
		TimePicker selectTime = (TimePicker) findViewById(R.id.selectTime);

    	//TimePicker ka kaam khatam
    	int min = selectTime.getCurrentMinute();
    	int hrs = selectTime.getCurrentHour();    	
    	
    	//EditText ka kaam khatam
    	EditText editDesc= (EditText) findViewById(R.id.editDesc);
    	String description = editDesc.getText().toString();
    	if(description.length()==0)
    	{
    		description="No Comments";
    	}
    	
    	calCalculate = Calendar.getInstance();
    	
	    switch (item.getItemId())
	    {
	        case R.id.done:
	        	//Save the alarm
	        	CheckBox repeatCheck = (CheckBox) findViewById(R.id.repeatTask);
	        	if(repeatCheck.isChecked())
	        	{
	        		//Days Repeat Option
	        		repeatType=0;
	        		calCalculate.set(Calendar.HOUR,hrs);
	        		calCalculate.set(Calendar.MINUTE, min);
	        		calCalculate.set(Calendar.SECOND, 0);
	        	}
	        	else
	        	{
	        		/***
	        		 * Now I have to check the repeatType here
	        		 * */
	        		
	        		RadioButton rb_none = (RadioButton) findViewById(222);
	        		RadioButton rb_monthly = (RadioButton) findViewById(333);
	        		RadioButton rb_yearly = (RadioButton) findViewById(444);
	        		if(rb_none.isChecked())
	        		{
	        			repeatType=1;
	        		}
	        		else if(rb_monthly.isChecked())
	        		{
	        			repeatType=2;
	        		}
	        		else if(rb_yearly.isChecked())
	        		{
	        			repeatType=3;
	        		}
	        		else
	        		{
	        			repeatType=0;
	        		}
	        		//Repeat Option Disabled repeat none, repeat monthly or repeat yearly
	        		DatePicker selectDate = (DatePicker) findViewById(111);
	        		int yrs = selectDate.getYear();
	        		int mon = selectDate.getMonth();
	        		int dat = selectDate.getDayOfMonth();
	        		calCalculate.set(yrs, mon, dat, hrs, min);
	        		calCalculate.set(Calendar.SECOND, 0);
	        	}
	        	newAlarm = new AlramRecord(description, day, repeatType, calCalculate);
	        	newAlarm.setAlarm();
	        	finish();
	    		return true;
	        case R.id.cancel:
	        	//delete function goes here
	            return true;
	        default:
	            return super.onOptionsItemSelected(item);
	    }
	}
	public void checkRepeat(View view)
	{
		final CheckBox repeatCheck = (CheckBox) findViewById(R.id.repeatTask);
		TableRow row4 = (TableRow) findViewById(R.id.Row4);
		TableRow row5 = (TableRow) findViewById(R.id.Row5);
		TableRow row6 = (TableRow) findViewById(R.id.Row6);
		if(repeatCheck.isChecked())
		{
			row4.removeAllViews();
			row5.removeAllViews();
			callDayPopup(AlarmCreator.this);
		}
		else
		{
			row6.removeAllViews();
			//if not selected display entry for date
			DatePicker selectDate = new DatePicker(this);
			selectDate.setId(111);
			
			RadioGroup repeatGroup = new RadioGroup(this);
			
			//Repeat None
			RadioButton repeatNone = new RadioButton(this);
			repeatNone.setChecked(true);
			repeatNone.setText("No Repeat");
			repeatNone.setId(222);
			
			//repeat Monthly
			RadioButton repeatMonthly = new RadioButton(this);
			repeatMonthly.setText("Repeat Monthly");
			repeatMonthly.setId(333);
			
			//Repeat Yearly
			RadioButton repeatYearly = new RadioButton(this);
			repeatYearly.setText("Repeat Yearly");
			repeatYearly.setId(444);
			
			//monthly repeat action listener
			repeatMonthly.setOnClickListener(new RadioButton.OnClickListener()
				{
					@Override
					public void onClick(View v)
					{
						boolean checked = ((RadioButton) v).isChecked();
					    switch(v.getId())
					    {
					        case 222:
					            if (checked)
					                repeatType=1;//Repeat None
					            break;
					        case 333:
					            if (checked)
					                repeatType=2;//Repeat Monthly
					            break;
					        case 444:
					        	if(checked)
					        		repeatType=3;//Repeat Yearly
					        	break;
					    }
					}
				}
			);
			
			//yearly repeat action listener
			repeatYearly.setOnClickListener(new RadioButton.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					boolean checked = ((RadioButton) v).isChecked();
				    switch(v.getId())
				    {
					    case 222:
				            if (checked)
				                repeatType=1;//Repeat None
				            break;
				        case 333:
				            if (checked)
				                repeatType=2;//Repeat Monthly
				            break;
				        case 444:
				        	if(checked)
				        		repeatType=3;//Repeat Yearly
				        	break;
				    }
				}
			}
		);
			

			//repeat none action listener
			repeatNone.setOnClickListener(new RadioButton.OnClickListener()
			{
				@Override
				public void onClick(View v)
				{
					boolean checked = ((RadioButton) v).isChecked();
				    switch(v.getId())
					{
					    case 222:
				            if (checked)
				                repeatType=1;//Repeat None
				            break;
				        case 333:
				            if (checked)
				                repeatType=2;//Repeat Monthly
				            break;
				        case 444:
				        	if(checked)
				        		repeatType=3;//Repeat Yearly
				        	break;
				    }
				}
			}
		);
			
			repeatGroup.addView(repeatNone);
			repeatGroup.addView(repeatMonthly);
			repeatGroup.addView(repeatYearly);
			
			//selectDate
			try
			{
				selectDate.setCalendarViewShown(false);
			}
			catch(Exception e)
			{}
			row4.addView(selectDate);
			row5.addView(repeatGroup);
		}
	}
	@SuppressWarnings("deprecation")
	public void callDayPopup(final Activity context)
	{
		int WindowWidth;
		int WindowHeight;
		Point p = new Point();
		Display screen = getWindowManager().getDefaultDisplay();
		try
		{
			screen.getSize(p);
			WindowHeight=p.y - 320;
			WindowWidth=p.x - 270;
		}
		catch(java.lang.NoSuchMethodError ignore)
		{
			WindowHeight=screen.getHeight() - 200;
			WindowWidth=screen.getWidth() - 200;
		}
		// Inflate the repeat_day.xml
		TableLayout viewGroup = (TableLayout) context.findViewById(R.id.popup);
		LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
		layout = layoutInflater.inflate(R.layout.repeat_day, viewGroup);

		PopupWindow popup = new PopupWindow(context);
		popup.setContentView(layout);
		popup.setWidth(WindowWidth);
		popup.setHeight(WindowHeight);
		popup.setFocusable(true);
		popup.showAtLocation(layout, Gravity.NO_GRAVITY,70,70);
		checkEnableBoxes();
		
	}
	public void checkEnableBoxes()
	{
		CheckBox[] checkPopupDay = new CheckBox[7];
		checkPopupDay[0] = (CheckBox) layout.findViewById(R.id.monday);
		checkPopupDay[1] = (CheckBox) layout.findViewById(R.id.tuesday);
		checkPopupDay[2] = (CheckBox) layout.findViewById(R.id.wednesday);
		checkPopupDay[3] = (CheckBox) layout.findViewById(R.id.thursday);
		checkPopupDay[4] = (CheckBox) layout.findViewById(R.id.friday);
		checkPopupDay[5] = (CheckBox) layout.findViewById(R.id.saturday);
		checkPopupDay[6] = (CheckBox) layout.findViewById(R.id.sunday);
		for(int i=0;i<7;i++)
		{
			if(day[i]==1)
			{
				checkPopupDay[i].setChecked(true);
			}
		}
		showDayString();
	}
	public void addDay(View view)
	{
		 boolean checked = ((CheckBox) view).isChecked();
		 switch(view.getId())
		 {
		 case R.id.sunday:
	        	if(checked)
	        	{
	        		day[0]=1;
	        	}
	        	else
	        	{
	        		day[0]=0;
	        	}
	        	break;
	        case R.id.monday:
	        	if(checked)
	        	{
	        		day[1]=1;
	        	}
	        	else
	        	{
	        		day[1]=0;
	        	}
	            break;
	        case R.id.tuesday:
	        	if(checked)
	        	{
	        		day[2]=1;
	        	}
	        	else
	        	{
	        		day[2]=0;
	        	}
	        	break;
	        case R.id.wednesday:
	        	if(checked)
	        	{
	        		day[3]=1;
	        	}
	        	else
	        	{
	        		day[3]=0;
	        	}
	        	break;
	        case R.id.thursday:
	        	if(checked)
	        	{
	        		day[4]=1;
	        	}
	        	else
	        	{
	        		day[4]=0;
	        	}
	        	break;
	        case R.id.friday:
	        	if(checked)
	        	{
	        		day[5]=1;
	        	}
	        	else
	        	{
	        		day[5]=0;
	        	}
	        	break;
	        case R.id.saturday:
	        	if(checked)
	        	{
	        		day[6]=1;
	        	}
	        	else
	        	{
	        		day[6]=0;
	        	}
	        	break;
	    }
		 showDayString();
	}
	public void showDayString()
	{
		TableRow row6 = (TableRow) findViewById(R.id.Row6);
		String[] days = {"Su","Mo","Tu","We","Th","Fr","Sa"};
		String showDay = new String();
		for(int i=0;i<7;i++)
		{
			if(day[i]==1)
			{
				showDay += days[i]+ " ";
			}
		}
		row6.removeAllViews();
		TextView showDayString = new TextView(this);
		showDayString.setText(showDay);
		row6.addView(showDayString);
	}
}
